package config

import (
	"github.com/spf13/viper"
	"log"
)

var (
	// Server
	ServerMode string
	ServerHost string
	ServerPort int
	// MySQL
	MySQLUser        string
	MySQLPassWord    string
	MySQLUrl         string
	MySQLPort        int
	MySQLDbName      string
	MySQLMaxIdleConn int
	MySQLMaxOpenConn int
	// Redis
	RedisAddr     string
	RedisPort     int
	RedisPassword string
	RedisDb       int
	// Email
	MailHost     string
	MailPort     int
	MailUser     string
	MailPassword string
	// Jwt
	JwtKey    string
	JwtIssuer string
)

func InitViperConfig() {
	viper.SetConfigName("config")
	viper.SetConfigType("yaml")
	viper.AddConfigPath("./")
	if err := viper.ReadInConfig(); err != nil {
		log.Println("读取配置文件错误，请检查路径，ERROR:", err)
	}
	loadDefault()
	loadServer()
	loadMySQL()
	loadRedis()
	loadMail()
	loadJwt()
}

// 默认配置
func loadDefault() {
	viper.SetDefault("server.mode", "debug")
	viper.SetDefault("server.host", "0.0.0.0")
	viper.SetDefault("server.port", 80)

	viper.SetDefault("mysql.user", "booking")
	viper.SetDefault("mysql.password", "password")
	viper.SetDefault("mysql.url", "localhost")
	viper.SetDefault("mysql.port", 3306)
	viper.SetDefault("mysql.dbName", "sport_booking")
	viper.SetDefault("mysql.maxIdleConn", 20)
	viper.SetDefault("mysql.maxOpenConn", 10)

	viper.SetDefault("redis.addr", "localhost")
	viper.SetDefault("redis.port", 6379)
	viper.SetDefault("redis.password", "booking")
	viper.SetDefault("redis.db", 0)

	viper.SetDefault("mail.host", "邮箱url")
	viper.SetDefault("mail.port", 994)
	viper.SetDefault("mail.user", "邮箱账户")
	viper.SetDefault("mail.password", "邮箱密码或授权码")

	viper.SetDefault("jwt.key", "密钥")
	viper.SetDefault("jwt.issuer", "签发人")

}
func loadServer() {
	ServerMode = viper.GetString("server.mode")
	ServerHost = viper.GetString("server.host")
	ServerPort = viper.GetInt("server.port")
}
func loadMySQL() {
	MySQLUser = viper.GetString("mysql.user")
	MySQLPassWord = viper.GetString("mysql.password")
	MySQLUrl = viper.GetString("mysql.url")
	MySQLPort = viper.GetInt("mysql.port")
	MySQLDbName = viper.GetString("mysql.dbName")
	MySQLMaxIdleConn = viper.GetInt("mysql.maxIdleConn")
	MySQLMaxOpenConn = viper.GetInt("mysql.maxOpenConn")
}
func loadRedis() {
	RedisAddr = viper.GetString("redis.addr")
	RedisPort = viper.GetInt("redis.port")
	RedisPassword = viper.GetString("redis.password")
	RedisDb = viper.GetInt("redis.db")
}
func loadMail() {
	MailHost = viper.GetString("mail.host")
	MailPort = viper.GetInt("mail.port")
	MailUser = viper.GetString("mail.user")
	MailPassword = viper.GetString("mail.password")
}
func loadJwt() {
	JwtKey = viper.GetString("jwt.key")
	JwtIssuer = viper.GetString("jwt.issuer")
}
